/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2022. All rights reserved.
 */

import path from 'path';

import { isLinux, isWindows } from '@ohos/hvigor';
import { Component } from '@ohos/sdkmanager-common';

import { CommonConst } from '../../const/common-const.js';
import { ToolChainsConst } from '../../const/sdk-const.js';
import { SdkVersionEnum } from '../../version/sdk-version-enum.js';
import { SdkComponent } from '../superior/sdk-component.js';

/**
 * sdk toolchains实例
 */
export class SdkToolchainsComponent extends SdkComponent {
  public static readonly MODULE_CHECK = 'modulecheck';
  public static readonly CONFIG_CHECK = 'configcheck';
  public static readonly SYS_CAP_CHECK = 'syscapcheck';
  public static readonly PAC_CHECK = 'paccheck';
  public static readonly LIB = 'lib';
  public static readonly LIBIMAGE_TRANSCODER_SHARED_DLL = 'libimage_transcoder_shared.dll';
  public static readonly LIBIMAGE_TRANSCODER_SHARED_DYLIB = 'libimage_transcoder_shared.dylib';
  public static readonly LIBIMAGE_TRANSCODER_SHARED_SO = 'libimage_transcoder_shared.so';
  public static readonly RICH_SCHEMA = 'configSchema_rich.json';
  public static readonly LITE_SCHEMA = 'configSchema_lite.json';

  constructor(component: Component) {
    super(component);
  }

  public getRestoolPath(): string {
    const resTool = isWindows() ? 'restool.exe' : 'restool';
    return path.resolve(this.getBaseDir(), resTool);
  }

  public getSysCapToolPath(): string {
    const sysCapTool = isWindows() ? 'syscap_tool.exe' : 'syscap_tool';
    return path.resolve(this.getBaseDir(), sysCapTool);
  }

  public getVerifySignConfigToolPath(): string {
    return path.resolve(this.getBaseDir(), SdkToolchainsComponent.LIB, 'hap-sign-tool.jar');
  }

  public getRichSchema(): string {
    return path.resolve(this.getBaseDir(), SdkToolchainsComponent.CONFIG_CHECK, SdkToolchainsComponent.RICH_SCHEMA);
  }

  public getLiteSchema(): string {
    return path.resolve(this.getBaseDir(), SdkToolchainsComponent.CONFIG_CHECK, SdkToolchainsComponent.LITE_SCHEMA);
  }

  public getPackageToolPath(): string {
    const packTool: string = this.getComponentVersion().isHigherThan(SdkVersionEnum.NEW_PACK_TOOL)
      ? ToolChainsConst.APP_PACKING_TOOL_JAR
      : ToolChainsConst.HMOS_APP_PACKING_TOOL_JAR;
    return path.resolve(this.getBaseDir(), SdkToolchainsComponent.LIB, packTool);
  }

  public getSignDir(): string {
    return path.resolve(this.getBaseDir(), SdkToolchainsComponent.LIB);
  }

  public getModuleSchema(): string {
    return path.resolve(this.getBaseDir(), SdkToolchainsComponent.MODULE_CHECK, CommonConst.MODULE_JSON);
  }

  public getAppSchema(): string {
    return path.resolve(this.getBaseDir(), SdkToolchainsComponent.MODULE_CHECK, CommonConst.APP_SCHEMA_JSON);
  }

  public getAppConfigurationSchema(): string {
    return path.resolve(this.getBaseDir(), SdkToolchainsComponent.MODULE_CHECK, CommonConst.CONFIGURATION_JSON);
  }

  public getInsightIntentSchema(): string {
    return path.resolve(this.getBaseDir(), SdkToolchainsComponent.MODULE_CHECK, CommonConst.INSIGHT_INTENT_SCHEMA_JSON);
  }

  public getArkDataSchema(): string {
    return path.resolve(this.getBaseDir(), SdkToolchainsComponent.MODULE_CHECK, CommonConst.ARK_DATA_SCHEMA_JSON);
  }

  public getUtdSchema(): string {
    return path.resolve(this.getBaseDir(), SdkToolchainsComponent.MODULE_CHECK, CommonConst.UTD_SCHEMA_JSON);
  }

  public getFormSchema(): string {
    return path.resolve(this.getBaseDir(), SdkToolchainsComponent.MODULE_CHECK, CommonConst.FORM_SCHEMA_JSON);
  }

  public getPageSchema(): string {
    return path.resolve(this.getBaseDir(), SdkToolchainsComponent.MODULE_CHECK, CommonConst.PAGE_SCHEMA_JSON);
  }

  public getRouterMapSchema(): string {
    return path.resolve(this.getBaseDir(), SdkToolchainsComponent.MODULE_CHECK, CommonConst.ROUTER_MAP_SCHEMA_JSON);
  }

  public getShortcutsJsonSchema(): string {
    return path.resolve(this.getBaseDir(), SdkToolchainsComponent.MODULE_CHECK, CommonConst.SHORTCUTS_SCHEMA_JSON);
  }

  public getAppStartupSchema(): string {
    return path.resolve(this.getBaseDir(), SdkToolchainsComponent.MODULE_CHECK, CommonConst.APP_STARTUP_SCHEMA_JSON);
  }

  public getHspStartupSchema(): string {
    return path.resolve(this.getBaseDir(), SdkToolchainsComponent.MODULE_CHECK, CommonConst.HSP_STARTUP_SCHEMA_JSON);
  }

  public getHarStartupSchema(): string {
    return path.resolve(this.getBaseDir(), SdkToolchainsComponent.MODULE_CHECK, CommonConst.HAR_STARTUP_SCHEMA_JSON);
  }

  getStartWindowSchema() {
    return path.resolve(this.getBaseDir(), SdkToolchainsComponent.MODULE_CHECK, CommonConst.START_WINDOW_SCHEMA_JSON);
  }

  public getSysCapSchema(): string {
    return path.resolve(this.getBaseDir(), SdkToolchainsComponent.SYS_CAP_CHECK, CommonConst.SYS_CAP_SCHEMA_JSON);
  }

  public getPacSchema(): string {
    return path.resolve(this.getBaseDir(), SdkToolchainsComponent.PAC_CHECK, CommonConst.PAC_SCHEMA_JSON);
  }

  getSystemThemeSchema() {
    return path.resolve(this.getBaseDir(), SdkToolchainsComponent.MODULE_CHECK, CommonConst.SYSTEM_THEME_JSON);
  }
}

export class HosSdkToolchainsComponent extends SdkToolchainsComponent {
  private component: Component;

  constructor(component: Component) {
    super(component);
    this.component = component;
  }

  public getModuleSchema(): string {
    return path.resolve(this.getBaseDir(), SdkToolchainsComponent.MODULE_CHECK, CommonConst.MODULE_JSON);
  }

  public getAppSchema(): string {
    return path.resolve(this.getBaseDir(), SdkToolchainsComponent.MODULE_CHECK, CommonConst.APP_SCHEMA_JSON);
  }

  public getRichSchema(): string {
    return path.resolve(this.getBaseDir(), SdkToolchainsComponent.CONFIG_CHECK, SdkToolchainsComponent.RICH_SCHEMA);
  }

  public getLiteSchema(): string {
    return path.resolve(this.getBaseDir(), SdkToolchainsComponent.CONFIG_CHECK, SdkToolchainsComponent.LITE_SCHEMA);
  }

  public getHapSignToolV2(): string {
    return path.resolve(this.getBaseDir(), HosSdkToolchainsComponent.LIB, ToolChainsConst.SIGN_TOOL);
  }

  public getHosModuleSchema(): string {
    return path.resolve(this.getBaseDir(), SdkToolchainsComponent.MODULE_CHECK, CommonConst.MODULE_JSON);
  }

  public getReleaseType(): string {
    return this.component.getReleaseType();
  }

  public getHosSignTool(): string {
    return path.resolve(this.getBaseDir(), HosSdkToolchainsComponent.LIB, ToolChainsConst.SDK_SIGN_TOOL_FULL);
  }

  public getHosToolchainsLibDir(): string {
    return path.resolve(this.getBaseDir(), HosSdkToolchainsComponent.LIB);
  }

  public getLibimageTranscoderShared(): string {
    const fileName = isWindows()
      ? HosSdkToolchainsComponent.LIBIMAGE_TRANSCODER_SHARED_DLL
      : isLinux()
        ? HosSdkToolchainsComponent.LIBIMAGE_TRANSCODER_SHARED_SO
        : HosSdkToolchainsComponent.LIBIMAGE_TRANSCODER_SHARED_DYLIB;
    return path.resolve(this.getHosToolchainsLibDir(), fileName);
  }
}
